package cn.bytecollege.user.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.*;

import java.util.List;

public class OrdersDetailDao extends BaseDao <OrderDetail,Integer>{
    /***
     * 根据当前商品信息生成订单信息
     */
    @Override
    public int save(OrderDetail orders) {
        String sql="insert into orders_detail(product_number , " +
                " product_id,user_id) values(?,?,?);";
        Object[] objects ={orders.getProductNumber(),
        orders.getProductId(),orders.getUserId()};
        return helper.executeUpdate(sql,objects);
    }
    /**
     * 根据用户姓名查找用户Id
     */
    public User findByname(String name){
        String sql="select user_id ,user_name,user_password,user_phonenumber from user where user_name=?";
        List<User> lists= helper.executeQuery(sql,User.class,name);
        User user=null;
        if(lists.size()>0){
            user=lists.get(0);
        }
        return user;
    }
    /**
     * 分页查询订单详情
     */

    public List<OrderList> findByPage(int pageIndex, int pageSize, int Id) {
        String sql="select orders_detail_id,o.product_number,o.product_id,o.user_id,product_name, " +
                " product_price,product_picture,product_particulars,product_allnumber,merchant_id,classify_id from orders_detail o inner join product p on o.product_id=p.product_id  where user_id=? limit ?,?;";
        Object[] objects1={Id,(pageIndex-1)*pageSize,pageSize};
        return helper.executeQuery(sql,OrderList.class,objects1);
    }
    /**
     * 查询用户Id所有订单详情
     */
    public List<OrderList> findByUserId(Integer Id){
        String sql="select orders_detail_id,o.product_number,o.product_id,o.user_id,product_name, " +
                " product_price,product_picture,product_particulars,product_allnumber,merchant_id,classify_id from orders_detail o inner join product p on o.product_id=p.product_id  where user_id=?   ";
        return helper.executeQuery(sql,OrderList.class,Id);
    }
    /**
     * 获取总条数
     */
    public int count(int id){
        String sql="select count(*) from orders_detail where user_id=?";
        return helper.executeScale(sql,id);
    }
    /**
     * 修改订单
     */
    @Override
    public int update(OrderDetail orderDetail) {
       String sql="update orders_detail set product_number=?,orders_id=? where orders_detail_id=?;";
       Object[] objects={orderDetail.getProductNumber(),orderDetail.getOrdersId(),orderDetail.getOrdersDetailId()};
       return helper.executeUpdate(sql,objects);
    }
    /**
     * 根据Id查找订单
     */
    @Override
    public OrderDetail findById(Integer id) {
        String sql="select  orders_detail_id,product_number,user_id,product_id,orders_id from orders_detail where orders_detail_id=? ";
        List<OrderDetail>lists= helper.executeQuery(sql,OrderDetail.class,id);
        OrderDetail orderDetail=lists.get(0);
        return orderDetail;
    }
    /**
     * 根据Id删除订单
     */
    @Override
    public int deleteById(Integer id) {
        String sql="delete from orders_detail where orders_detail_id=?;";
        return helper.executeUpdate(sql,id);
    }
    /**
     * 根据用户Id查询所有的订单
     */
    public List<OrderList> orderLists(Integer id){
        String sql="select orders_detail_id,o.product_number,o.product_id,o.user_id,product_name, orders_id ," +
        " product_price,product_picture,product_particulars,product_allnumber,merchant_id,classify_id from orders_detail o inner join product p on o.product_id=p.product_id  where user_id=?	AND o.orders_id is null;";
        return helper.executeQuery(sql,OrderList.class,id);
    }
    /**
     * 根据产品Id和购买数量为零的条件查询订单
     */
    public OrderDetail findByProductId(Integer id){
        String sql="select orders_detail_id,product_number,user_id,product_id,orders_id from orders_detail where product_id=? and product_number is null;";
        List<OrderDetail> orderDetailList=helper.executeQuery(sql,OrderDetail.class,id);
        OrderDetail orderDetail=null;
        if(orderDetailList.size()!=0){
            orderDetail=orderDetailList.get(0);
        }
        return orderDetail;

    }
}
